home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / libsrc / c / sys / utime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-18  |  1.8 KB  |  65 lines

  1. /*
  2.   (c) Copyright 1992 Eric Backus
  3.  
  4.   This software may be used freely so long as this copyright notice is
  5.   left intact.  There is no warrantee on this software.
  6. */
  7.  
  8. #include <time.h>        /* For localtime() */
  9. #include <fcntl.h>        /* For open() */
  10. #include <dos.h>        /* For intdos() */
  11. #include <errno.h>        /* For errno */
  12. #include <utime.h>        /* For utime() */
  13.  
  14. /* An implementation of utime() for DJGPP.  The utime() function
  15.    specifies an access time and a modification time.  DOS has only one
  16.    time, so we will (arbitrarily) use the modification time. */
  17. int
  18. utime(const char *path, const struct utimbuf *times)
  19. {
  20.     union REGS        regs;
  21.     struct tm        *tm;
  22.     time_t        modtime;
  23.     int            fildes;
  24.     unsigned int    dostime, dosdate;
  25.  
  26.     /* DOS wants the file open */
  27.     fildes = open(path, O_RDONLY);
  28.     if (fildes == -1) return -1;
  29.  
  30.     /* NULL times means use current time */
  31.     if (times == NULL)
  32.     modtime = time((time_t *) 0);
  33.     else
  34.     modtime = times->modtime;
  35.  
  36.     /* Convert UNIX time to DOS date and time */
  37.     tm = localtime(&modtime);
  38.     if (tm->tm_year < 80) tm->tm_year = 80; /* DOS starts at 1980 */
  39.     dosdate = tm->tm_mday + ((tm->tm_mon + 1) << 5) +
  40.     ((tm->tm_year - 80) << 9);
  41.     dostime = tm->tm_sec / 2 + (tm->tm_min << 5) +
  42.     (tm->tm_hour << 11);
  43.  
  44.     /* Set the file timestamp */
  45.     regs.h.ah = 0x57;        /* DOS FileTimes call */
  46.     regs.h.al = 0x01;        /* Set date/time request */
  47.     regs.x.bx = fildes;        /* File handle */
  48.     regs.x.cx = dostime;    /* New time */
  49.     regs.x.dx = dosdate;    /* New date */
  50.     (void) intdos(®s, ®s);
  51.  
  52.     /* Close the file */
  53.     (void) close(fildes);
  54.  
  55.     /* Return */
  56.     if (regs.x.cflag != 0)
  57.     {
  58.     /* Not sure this is even possible. */
  59.     errno = regs.x.ax;
  60.     return -1;
  61.     }
  62.     else
  63.     return 0;
  64. }
  65.